<template>
  <div class="container">
    <div class="header">
      <el-input v-model="params.name" placeholder="请输入日志标题" class="input" style="width: 300px;"></el-input>
      <el-button @click="findBySearch" class="button">查询</el-button>
      <el-button @click="reset" class="button">清空</el-button>
      <el-button @click="add" class="button">新增</el-button>
    </div>
    <div class="table-container">
      <el-table :data="tableData" class="table">
        <el-table-column prop="name" label="日志标题" width="180"></el-table-column>
        <el-table-column prop="content" label="日志内容" width="550"></el-table-column>
        <el-table-column prop="time" label="日志发布时间" width="180"></el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="请填写信息" :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="form" class="dialog-form">
        <el-form-item label="日志标题" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" class="input" style="width: 100%;"></el-input>
        </el-form-item>
        <el-form-item label="日志内容" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="form.content" autocomplete="off" class="input" style="width: 100%;"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  data() {
    return {
      params: {
        name: '',
        pageNum: 1,
        pageSize: 5,
      },
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form: {},
      formLabelWidth: '80px',
      user: localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : {},
      loading: false
    };
  },
  created() {
    this.findBySearch();
  },
  methods: {
    findBySearch() {
      this.loading = true;
      request.get("/log/search", {
        params: this.params
      }).then(res => {
        this.loading = false;
        if (res.code === '0') {
          this.tableData = res.data.list;
          this.total = res.data.total;
        } else {
          this.$message.error("查询失败");
        }
      }).catch(() => {
        this.loading = false;
        this.$message.error("网络异常");
      });
    },
    del(id) {
      request.delete("/log/" + id).then(res => {
        if (res.code === "0") {
          this.$message.success("删除成功");
          this.findBySearch();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    add() {
      this.form = {};
      this.dialogFormVisible = true;
    },
    edit(obj) {
      this.form = obj;
      this.dialogFormVisible = true;
    },
    reset() {
      this.params = {
        name: '',
        pageNum: 1,
        pageSize: 5
      };
      this.findBySearch();
    },
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.findBySearch();
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum;
      this.findBySearch();
    },
    submit() {
      this.loading = true;
      request.post("/log", this.form).then(res => {
        this.loading = false;
        if (res.code === "0") {
          this.$message.success("操作成功");
          this.dialogFormVisible = false;
          this.findBySearch();
        } else {
          this.$message.error("操作失败");
        }
      }).catch(() => {
        this.loading = false;
        this.$message.error("网络异常");
      });
    }
  }
};
</script>

<style scoped>
.container {
  display: flex;
  flex-direction: column;
  gap: 15px;
  padding: 20px;
  background-color: #f5f5f5;
  border-radius: 8px;
}

.header {
  display: flex;
  gap: 10px;
}

.input {
  border: 1px solid #ccc;
  border-radius: 4px;
}

.button {
  background-color: #409eff;
  color: white;
  border-color: #409eff;
}

.button:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

.table-container {
  background-color: white;
  border-radius: 8px;
  padding: 10px;
}

.pagination {
  display: flex;
  justify-content: center;
}

.dialog-form {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.el-dialog__header {
  background-color: #f5f5f5;
  color: #333;
  padding: 15px;
  border-bottom: 1px solid #ddd;
}

.el-dialog__body {
  padding: 20px;
}

.table {
  --el-table-header-bg-color: #f5f5f5;
  --el-table-row-hover-bg-color: #f0f0f0;
}
</style>